package com.tfjybj.itoo.exam.provider.controller;

import com.dmsdbj.itoo.tool.business.ItooResult;
import com.github.pagehelper.PageInfo;
import com.tfjybj.itoo.exam.entity.CourseEntity;
import com.tfjybj.itoo.exam.model.CourseModel;
import com.tfjybj.itoo.exam.provider.service.CourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * CourseController
 * course表
 *
 * @author 杨江涛
 * @version 2.0.0
 * @since 2.0.0 2018-11-24 15:14:12
 */
@Api(tags = {"course表接口"})
@RequestMapping(value = "/course")
@RestController
public class CourseController {

    @Resource
    private CourseService courseService;

    //region 模板生成：基本增删改

    /**
     * 添加
     *
     * @param model CourseModel
     * @return 添加的结果
     * @author 杨江涛
     * @since 2.0.0 2018-11-24 15:14:12
     */
    @ApiOperation(value = "添加")
    @PostMapping(value = {"/create"})
    public ItooResult create(@RequestBody CourseModel model) {
        if (StringUtils.isEmpty(model.getName())) {
            return ItooResult.build(ItooResult.FAIL, "name为空");
        }
        if (StringUtils.isEmpty(model.getCode())) {
            return ItooResult.build(ItooResult.FAIL, "code为空");
        }
        CourseEntity courseEntity = new CourseEntity();
        BeanUtils.copyProperties(model, courseEntity);
        courseService.save(courseEntity);
        return ItooResult.build(ItooResult.SUCCESS, "添加成功");
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @return ItooResult 是否删除成功
     * @author 杨江涛
     * @since 2.0.0 2018-11-24 15:14:12
     */
    @ApiOperation(value = "根据id删除（单个）")
    @DeleteMapping(value = {"/delete/{id}"})
    public ItooResult delete(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        courseService.removeById(id);
        return ItooResult.build(ItooResult.SUCCESS, "删除成功");
    }

    /**
     * 批量删除
     *
     * @param ids ids
     * @return ItooResult 批量删除是否成功结果
     * @author 杨江涛
     * @since 2.0.0 2018-11-24 15:14:12
     */
    @ApiOperation(value = "根据id批量删除")
    @DeleteMapping(value = {"/deleteByIds"})
    @ApiImplicitParam(name = "ids", value = "ids", dataType = "List<String>", required = true)
    public ItooResult deleteByIds(@RequestBody List<String> ids) {
        courseService.removeByIds(ids);
        return ItooResult.build(ItooResult.SUCCESS, "批量删除成功");
    }

    /**
     * 修改
     *
     * @param model CourseModel
     * @return 修改后的结果
     * @author 杨江涛
     * @since 2.0.0 2018-11-24 15:14:12
     */
    @ApiOperation(value = "根据id修改course")
    @PutMapping(value = {"/modify"})
    public ItooResult modify(@RequestBody CourseModel model) {
        if (StringUtils.isEmpty(model.getName())) {
            return ItooResult.build(ItooResult.FAIL, "name为空");
        }
        if (StringUtils.isEmpty(model.getCode())) {
            return ItooResult.build(ItooResult.FAIL, "code为空");
        }
        CourseEntity courseEntity = new CourseEntity();
        BeanUtils.copyProperties(model, courseEntity);
        courseService.updateById(courseEntity);
        return ItooResult.build(ItooResult.SUCCESS, "修改成功");
    }

    /**
     * 根据id查找Course
     *
     * @param id 主键id
     * @return 根据id查找的结果
     * @author 杨江涛
     * @since 2.0.0 2018-11-24 15:14:12
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping(value = {"/findById/{id}"})
    public ItooResult findById(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        CourseEntity courseEntity = courseService.getById(id);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", courseEntity);
    }

    /**
     * 分页查询所有Course
     *
     * @param pageNo   页码
     * @param pageSize 每页条数
     * @return 分页查询的结果
     * @author 杨江涛
     * @since 2.0.0 2018-11-24 15:14:12
     */
    @ApiOperation(value = "分页查询所有Course")
    @GetMapping(value = {"/queryPageAll/{pageNo}/{pageSize}"})
    public ItooResult queryPageAll(@ApiParam(name = "pageNo", value = "页码", required = true, example = "1") @PathVariable Integer pageNo,
                                   @ApiParam(name = "pageSize", value = "页数", required = true, example = "10") @PathVariable Integer pageSize) {
        PageInfo<CourseEntity> courses = courseService.queryPageAll(pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", courses);
    }

    //endregion

    /* **********************************以下为非模板生成的内容********************************* */

    /**
     * 无参查询课程信息Course表当前学年课程信息
     *
     * @return Course表当前学年课程信息
     * @author 杨江涛
     * @since 2.0.0 2018-11-24 15:14:12
     */
    @ApiOperation(value = "无参查询课程信息Course表当前学年课程信息")
    @GetMapping(value = {"/findCourse"})
    public ItooResult findCourse() {
        List<CourseEntity> courseEntityList = courseService.findCourse();
        return ItooResult.build(ItooResult.SUCCESS, "无参查询当前学年课程信息成功", courseEntityList);
    }

    /**
     * 根据id查找Course表
     *
     * @param id 主键id
     * @return 课程信息
     * @author 杨江涛
     * @since 2.0.0  2018-11-23 20:42:04
     */
    @ApiOperation(value = "根据id查询课程名称返回string类型")
    @GetMapping(value = {"/findCourseById/{id}"})
    public ItooResult findCourseById(@ApiParam(value = "课程id", required = true) @PathVariable String id) {
        String strCourse = courseService.findByCourseId(id);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", strCourse);
    }

    /**
     * 根据id查询课程名称返回list类型
     *
     * @param id 主键id
     * @return 课程信息
     * @author 杨江涛
     * @since 2.0.0  2018-12-4 17:06:08
     */
    @ApiOperation(value = "根据id查询课程名称返回list类型")
    @GetMapping(value = {"/getByCourseId/{id}"})
    public ItooResult getByCourseId(@ApiParam(value = "课程id", required = true) @PathVariable String id) {
        List<CourseEntity> courseEntityList = courseService.getByCourseId(id);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", courseEntityList);
    }

    /**
     * 根据参数模糊查询课程名称返回list类型
     * @param pageNo  页码
     * @param pageSize 每页条数
     * @param searchInfo 模糊查询条件（课程名称/课程代码）searchInfo
     * @return 根据参数模糊查询课程名称返回list类型
     * @author 杨江涛
     * @since 2.0.0  2018-12-6 14:50:58
     */
    @ApiOperation(value = "根据参数模糊查询课程名称返回list类型")
    @GetMapping(value = "/getCourse/{searchInfo}/{pageNo}/{pageSize}")
    public ItooResult getCourse(
            @ApiParam(value = "房屋查询参数", required = true) @PathVariable String searchInfo,
            @ApiParam(name = "pageNo", value = "页码", required = true, example = "1") @PathVariable Integer pageNo,
            @ApiParam(name = "pageSize", value = "页数", required = true, example = "10") @PathVariable Integer pageSize) {

        PageInfo<CourseEntity> courseEntityList = courseService.getCourse(searchInfo.trim(), pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", courseEntityList);
    }

    /**
     * 创建考试查询课程,无参查询
     *
     * @author maying
     * @since  2018-12-8 21:42:00
     * @return courseEntityList
     */

    @ApiOperation(value = "创建考试查询课程,无参查询")
    @GetMapping(value = "/inquiryCourse/{userId}")
    public ItooResult inquiryCourse(@PathVariable(required = false) String userId) {
        List<CourseEntity> courseEntityList=courseService.inquiryCourse(userId);
        return ItooResult.build(ItooResult.SUCCESS,"查询成功!",courseEntityList);
    }

}
